﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UI
{
    public class UIActivityTemplate : MonoBehaviour
    {
        public UIActivity activity;
        /// <summary>
        /// 活动类型
        /// </summary>
        public ActivityType activityType;

        /// <summary>
        /// 活动id
        /// </summary>
        public string activityId;

        //动画效果
        public List<TweenPosition> posTweeners;
        public List<TweenAlpha> alphaTweeners;

        /// <summary>
        /// 图标cached
        /// </summary>
        protected Dictionary<string, GameObject> cached = new Dictionary<string, GameObject>();

        protected bool isPlayOnce = true;

        void Awake()
        {
            InitOnce();
        }

        void Update()
        {
            DoUpdate();
        }
        public virtual void Init(UIActivity activity)
        {
            this.activity = activity;

            Clear();
            PrepareData();
            PrepareView();
        }

        
        /// <summary>
        /// 一次性初始化
        /// </summary>
        protected virtual void InitOnce()
        {

        }

        /// <summary>
        /// 每一针更新
        /// </summary>
        protected virtual void DoUpdate() { }
        

        public void Clear()
        {
            if(cached.Count>0)
            {
                foreach(var kvp in cached)
                {
                    GameObject.Destroy(kvp.Value);
                }
            }

            cached.Clear();
        }

        /// <summary>
        /// 准备数据，来自网络或者本地
        /// </summary>
        protected virtual void PrepareData()
        {
            this.activityType = ActivityType.None;
        }

        /// <summary>
        /// 准备显示
        /// </summary>
        protected virtual void PrepareView()
        {
        }

        /// <summary>
        /// 刷新Icon
        /// </summary>
        protected virtual void RefreshAllIcons()
        {
        }

        /// <summary>
        /// 建立Icons
        /// </summary>
        protected virtual void RebuildAllIcons()
        {
        }

        public virtual void Refersh()
        {

        }
        
        /// <summary>
        /// 建立Icon
        /// </summary>
        /// <param name="id"></param>
        /// <param name="temp"></param>
        /// <returns></returns>
        protected GameObject GetIcon(string id, GameObject temp)
        {
            if(cached.ContainsKey(id))
            {
                return cached[id];
            }
            else
            {
                GameObject inst = GameObject.Instantiate(temp);
                cached[id] = inst;
                return inst;
            }
        }

        /// <summary>
        /// 显示面板
        /// </summary>
        public virtual void Show()
        {
            if (activity == null)
                return;
            gameObject.SetActive(true);

           

            if (isPlayOnce)
            {
                isPlayOnce = false;
                if (activity.showTimes == 0)
                {
                    foreach (var a in alphaTweeners) a.delay = 0.5f;
                    foreach (var pos in posTweeners) pos.delay = 0.5f;
                }
                else
                {
                    foreach (var a in alphaTweeners) a.delay = 0.1f;
                    foreach (var pos in posTweeners) pos.delay = 0.1f;
                    
                    //为了屏蔽效果
                    return;
                }

                float delayTime = 0f;

                List<UITweener> tweeners = new List<UITweener>();

                foreach (var tweenAlpha in alphaTweeners) tweeners.Add(tweenAlpha);
                foreach (var tweenPos in posTweeners) tweeners.Add(tweenPos);
                

                foreach (var tweener in tweeners)
                {
                    tweener.ResetToBeginning();
                    tweener.PlayForward();
                    delayTime = Mathf.Max(delayTime, tweener.delay + tweener.duration);
                }

                if (Mathf.Approximately(delayTime, 0f))
                {
                    ShowContent();
                    ShowButtons();
                }
                else
                {
                    StopCoroutine("_DelayShow");
                    StartCoroutine(_DelayShow(delayTime));
                }
            }
        }

        private IEnumerator _DelayShow(float delay)
        {
            yield return new WaitForSeconds(delay);
            ShowContent();
            ShowButtons();
        }
        
        /// <summary>
        /// 在tween完成时候显示内容
        /// </summary>
        protected virtual void ShowContent()
        {

        }

        /// <summary>
        /// 当Tween完成显示按钮组件
        /// </summary>
        protected virtual void ShowButtons()
        {

        }

        /// <summary>
        /// 隐藏面板
        /// </summary>
        public virtual void Hide()
        {
            gameObject.SetActive(false);
        }

        /// <summary>
        /// 页面关闭调用
        /// </summary>
        public virtual void OnPageClosed()
        {

        }

        /// <summary>
        /// 弹窗页面去除时候调用
        /// </summary>
        public virtual void OnCoverPageRemove()
        {

        }
        /// <summary>
        /// 浮动窗口打开时调用
        /// </summary>
        public virtual void OnCoverPageOpen()
        {
        }
    }
}